/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

var osapps = osapps || {};
osapps.ui = osapps.ui || {};

osapps.ui.Render = {
  debug : true,
  
  templates : {},
  
  setTemplate : function (templateId, template) {
    templates[templateId] = template;
  },
  
  setTemplates : function (templates) {
    osapps.mixin(this.templates, templates);
  },
  
  render: function (templateId, opt_args) {
    if(!opt_args){
  		return this.templates[templateId].process(window);
  	}else{
		opt_args._MODIFIERS = window._MODIFIERS;
  		return this.templates[templateId].process(opt_args);
  	}
  },

  rootElement: null,
  setRootElement : function(rootElement) {
    this.rootElement = rootElement;
  },

  displayingPage: "Home",
  setDisplayingPage : function(pageId) {
    this.displayingPage = pageId;
  },
  showPage : function(pageId) {
    this.displayingPage = pageId;
    this.refreshUI();
  },

  clearUI : function(opt_element){
    opt_element = opt_element || this.rootElement;
    if(opt_element) {
      //clear
      while (opt_element.childNodes[0]) {
        opt_element.removeChild(opt_element.childNodes[0]);
      }
    }
  },
  
  refreshUI : function() {
    this.clearUI(this.rootElement);
    this.rootElement.innerHTML = this.render(this.displayingPage);
    if(gadgets && gadgets.window && gadgets.window.adjustHeight && (gadgets.window.adjustHeight instanceof Function)) {
      gadgets.window.adjustHeight();
    } 
  },

  showDialog : function(dialogId) {
    var dlg = document.getElementById("dialogRoot");
    if (dlg) {
      this.closeDialog();
    }
    
    dlg = document.createElement("span");
    dlg.setAttribute("id", "dialogRoot");
    dlg.innerHTML = osapps.ui.Render.render(dialogId);
    this.rootElement.appendChild(dlg);
	//adjust top
    var top = 100 + document.body.scrollTop + "px";
    var dialogBox = document.getElementById("dialogBox");
    dialogBox.style.top = top;
    var bg = document.getElementById("dialogBoxShadow");
    bg.style.top = document.body.scrollTop + "px";
  },
  closeDialog : function() {
    var dlg = document.getElementById("dialogRoot");
    if (dlg) {
      this.clearUI(dlg);
      this.rootElement.removeChild(dlg);
    }
  },
  
  msgBox : function(text,callback){
    var dlg = document.getElementById("dialogRoot");
    if (dlg) {
      this.closeDialog();
    }
    
    dlg = document.createElement("span");
    dlg.setAttribute("id", "dialogRoot");
    dlg.innerHTML = osapps.ui.Render.render("MessageBox",{innerHTML:text});
    this.rootElement.appendChild(dlg);
	//adjust top
    var top = 100 + document.body.scrollTop + "px";
    var dialogBox = document.getElementById("dialogBox");
    dialogBox.style.top = top;
    var bg = document.getElementById("dialogBoxShadow");
    bg.style.top = document.body.scrollTop + "px";
	
	if (callback && typeof(callback) == "function") {
			document.getElementById("submit").onclick = callback;
	}
  },
  
  //custom modifiers for ${"templateId"|template:data} and ${"messageId"|MSG}
  templateModifiers : {
	template : function(str,obj){
		return osapps.ui.Render.render(str,obj);
	},
	MSG : function(str,msg){
		if (gadgets && gadgets.Prefs) {
              return new gadgets.Prefs().getMsg(str);
        } else {
              return "MSG:{" + str + "}";
        }
	}
  }

};

window._MODIFIERS = osapps.ui.Render.templateModifiers;
